#include <iostream>
#include <io.h>
#include <direct.h>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main() {
    string basic_path = "D:\\MyProject\\RMWalker\\binocular_pic\\pic_roll2";    //双目图片路径
    string dst_path   = basic_path + "\\dst";                                   //图片保存目标路径
    string dst_path_l = dst_path + "\\l\\";
    string dst_path_r = dst_path + "\\r\\";
    Mat    img_src, img_l, img_r;
    Rect   rect_r, rect_l;
    int    n    = 15;   //双目图片的张数
    int    rows = 0, cols = 0, mid = 0;
    int    cnt = 0;

    try {
        if(0 != _access(dst_path.c_str(), 0)) assert(!_mkdir((dst_path.c_str())));  //目录创建成功，则返回0；否则返回 - 1
        if(0 != _access(dst_path_l.c_str(), 0)) assert(!_mkdir((dst_path_l.c_str())));
        if(0 != _access(dst_path_r.c_str(), 0)) assert(!_mkdir((dst_path_r.c_str())));
        for(int i = 1; i <= n; i++) {
            img_src = imread(basic_path + "\\" + to_string(i) + ".jpg");
            cout << basic_path + "\\" + to_string(i) + ".jpg : " << endl;
            // imshow("review_src", img_src); //预览原图
            rows = img_src.rows;
            cols = img_src.cols;
            mid  = cols / 2;                                    //中间分界坐标

            // Rect([起始x坐标],[起始y坐标],[宽度],[高度])
            img_l = Mat(img_src, Rect(0, 0, mid, rows));        //生成左目图
            img_r = Mat(img_src, Rect(mid, 0, mid, rows));      //生成右目图

            //保存左目图和右目图
            cout << "img_l: cols=" << img_l.cols << " rows=" << img_l.rows;
            if(imwrite(dst_path_l + "l" + to_string(i) + ".jpg", img_l)) {
                cnt++;
                cout << "write:1";
            }
            else {
                cout << "write:0";
            }
            cout << "img_r: cols=" << img_r.cols << " rows=" << img_r.rows;
            if(imwrite(dst_path_r + "r" + to_string(i) + ".jpg", img_r)) {
                cnt++;
                cout << " write:1";
            }
            else {
                cout << " write:0";
            }
            cout << endl;
            // imshow("review_s", img_src);imshow("review_1", img_l);imshow("review_2", img_r);//预览原图，左目图，右目图
        }
    }
    catch(const cv::Exception& ex) {
        cerr << ex.what() << endl;
    }
    
    cout << "\n已写入" << cnt << "张图片" << endl; 
    //waitKey(0);
	return 0;
}

//int imgcut(const string folder_path, int n = 1, string dst_path = "") { 
//    int    cnt = 0;
//    if(dst_path == "") dst_path = folder_path + "\\dst";
//    string dst_path_l = dst_path + "\\l\\";
//    string dst_path_r = dst_path + "\\r\\";
//    if(0 != access(dst_path.c_str(), 0)) assert(!mkdir((dst_path.c_str())));  //目录创建成功，则返回0；否则返回 - 1
//    if(0 != access(dst_path_l.c_str(), 0)) assert(!mkdir((dst_path_l.c_str())));
//    if(0 != access(dst_path_r.c_str(), 0)) assert(!mkdir((dst_path_r.c_str())));
//    for(int i = 1; i <= n; i++) {
//        Mat img_src = imread(folder_path + "\\" + to_string(i) + ".jpg");  //注意图片名称格式
//        if(imwrite(dst_path_l + "l" + to_string(i) + ".jpg", Mat(img_src, Rect(0, 0, img_src.cols / 2, img_src.rows)))) cnt++;
//        if(imwrite(dst_path_r + "r" + to_string(i) + ".jpg", Mat(img_src, Rect(img_src.cols / 2, 0, img_src.cols / 2, img_src.rows)))) cnt++;
//    }
//    return cnt;
//}
//
//int main() { 
//    imgcut("D:\\MyProject\\RMWalker\\binocular_pic\\pic_roll\\", 21);
//    return 0;
//}